<?php
namespace Modules\Home\Controllers;

use Common\Models\UserModel;
use Common\Models\UserShopModel;

class ServiceCenterController extends ControllerBase {
  public function initialize() {
    parent::initialize();

    $this -> _checkLogin();
    $this -> view -> setLayout('index');
  }

  public function indexAction() {
    $this -> tag -> prependTitle('服务中心');

    $user = $this -> session -> get('user');

    $this -> view -> currentUser = $user;
    $findFirstUserShop = UserShopModel::findFirst('user_id = ' . $user['user_id']);
    if($findFirstUserShop) {
      $this -> view -> userShop = $findFirstUserShop;
      switch($findFirstUserShop -> status) {
        case 1:
          $this -> view -> pick('ServiceCenter/index2');
          break;
        case 2:
          $this -> view -> pick('ServiceCenter/index3');
          break;
        case 9:
          if($findFirstUserShop -> shop_level == 2) {
            $this -> view -> pick('ServiceCenter/index4');
          } else if($findFirstUserShop -> shop_level == 3) {
            $this -> view -> pick('ServiceCenter/index5');
          }
      }
    } else {
      $this -> tag -> prependTitle('开店');
      $this -> view -> pick('ServiceCenter/index1');
    }
  }

  public function createAction() {
    $validation = (new \Modules\Home\Validations\UserShopValidation()) -> create($this -> request -> getPost());
    if($validation['code'] != 0) {
      return $this -> _returnScript($validation['message'], '/service/center');
    }

    $UserShopModel = new UserShopModel($validation['post']);
    if(!$UserShopModel -> create()) {
      return $this -> _returnScript('申请失败', '/service/center');
    }

    return $this -> _returnScript('申请成功', '/service/center');
  }

  public function statusAction($status) {
    $user = $this -> session -> get('user');
    $findFirstUserShop = UserShopModel::findFirst('user_id = ' . $user['user_id']);
    $findFirstUserShop -> status = $status;
    if(!$findFirstUserShop -> save()) {
      return $this -> _returnScript('申请失败', '/service/center');
    }

    return $this -> _returnScript('申请成功', '/service/center');
  }

  public function shopLevelAction() {
    $user = $this -> session -> get('user');
    $post = $this -> request -> getPost();
    $findFirstUserShop = UserShopModel::findFirst('user_id = ' . $user['user_id']);
    if($post['shop_level'] <= $findFirstUserShop -> shop_level) {
      return $this -> _returnScript('不能低于当前等级', '/service/center');
    }

    $findFirstUserShop -> status = 1;
    $findFirstUserShop -> shop_level = $post['shop_level'];
    if(!$findFirstUserShop -> save()) {
      return $this -> _returnScript('申请失败', '/service/center');
    }

    return $this -> _returnScript('申请成功', '/service/center');
  }

  public function deleteAction() {
    $user = $this -> session -> get('user');
    $findFirstUser = UserModel::findFirst('user_id = ' . $user['user_id']);
    $findFirstUser -> shop_level = 1;
    if(!$findFirstUser -> save()) {
      return $this -> _returnScript('删除失败', '/service/center');
    }

    $findFirstUserShop = UserShopModel::findFirst('user_id = ' . $user['user_id']);
    if(!$findFirstUserShop -> delete()) {
      return $this -> _returnScript('删除失败', '/service/center');
    }

    $this -> session -> set('user', $findFirstUser -> toArray());
    return $this -> _returnScript('删除成功', '/service/center');
  }
}
?>